package com.lzy.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;

@Slf4j
public class HtmlUtil {
    /**
     * 获取url页面内容
     *
     * @param url
     * @return
     */
    public static String getHtml(String url) {
        String html = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        httpget.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 7.1.1; MI 6 Build/NMF26X; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/043807 Mobile Safari/537.36 MicroMessenger/6.6.1.1220(0x26060135) NetType/4G Language/zh_CN MicroMessenger/6.6.1.1220(0x26060135) NetType/4G Language/zh_CN miniProgram");
        try {
            HttpResponse responce = httpClient.execute(httpget);
            int status = responce.getStatusLine().getStatusCode();
            if (status == HttpStatus.SC_OK) {
                HttpEntity entity = responce.getEntity();
                if (entity != null) {
                    html = EntityUtils.toString(entity);
                }
            }
        } catch (Exception e) {
            log.error("获取url:" + url + ",页面数据失败", e);
            throw new RuntimeException("获取url失败", e);
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error("关闭httpClient失败", e);
            }
        }
        return html;
    }

    /**
     * 获取 html中的纯文本
     *
     * @param html html字符串
     * @return
     */
    public static String getText(String html) {
        if (StringUtils.isEmpty(html)) {
            return "";
        }
        Document document = Jsoup.parse(html);
        return document.text();
    }

    public static String removeTagA(String html) {
        if (StringUtils.isEmpty(html)) {
            return "";
        }
        Document document = Jsoup.parse(html);
        Elements tabs = document.getElementsByTag("a");
        if (CollectionUtils.isEmpty(tabs)) {
            return document.html();
        }
        for (Element element : tabs) {
            tabs.attr("href", "javaScript:void(0)");
        }
        return document.html();
    }




}
